package systems.rpgBuilder;

import java.util.ArrayList;
import java.util.List;

import org.jdom.Element;

import systems.languages.MultiLanguages;

/**
 * Provides an easy way to create and handle a player Classe according to the
 * RpgBuilder configuration file.
 * @see RpgBuilder class to get more information about the use of the builder.
 * @author Baptiste Legrand & Benjamin Orman
 * @version 1.0
 */
public class Classe {
  //The classe unique identifier
  private String id;
  //The multilanguage property key
  private String multiLanguageName;
  //The name of the table where the classe information are store.
  private String tableName;
  //The path to the picture of the classe.
  private String imagePath;
  //The minimum level for a character in the game
  private int minLvl;
  //The maximum level for a character in the game
  private int maxLvl;
  //The Array of String containing the list of the faction id that this class 
  //can play
  private String[] factionsId;
  //The List of the subclasse that a character of this classe can be in the 
  //futur
  //Optimization : As the is only one creation and multiple access (get) the
  //ArrayList shall be used instead of the linked list.
  private List<Classe> subClassesList;

  /**
   * Constructor creates a Classe according to the XML Element given in 
   * parameter.
   * @param classe
   */
  public Classe(Element classe)
  {
    //Get the unique identifier
    this.id  = classe.getAttributeValue("id");
    //Get the faction id.
    this.factionsId = classe.getAttributeValue("factionId").split(" ");
    //Get the multilanguage property name.
    this.multiLanguageName = classe.getChildText("multiLangueName");
    //Get the table name where to get the classe data.
    this.tableName = classe.getChildText("tableName");
    //Get the path of the picture of this classe.
    this.imagePath = classe.getChildText("imagePath");
    //Get the minLvl require for this classe.
    this.minLvl = Integer.valueOf(classe.getChildText("minLevel"));
    //Get the maximum level you can reach with this classe.
    this.maxLvl = Integer.valueOf(classe.getChildText("maxLevel"));
    //Now we will generate the subClassList
    List<Element> tmpSubClasses = classe.getChildren();
    // if there is more than 5 elements (which means there is, at least, 1 
    // subclass) ...
    if(tmpSubClasses.size() > 5) 
    { 
      //... starting with removin all the previous data collected
      for(int i=0; i<5; i++)
        tmpSubClasses.remove(0);

      //... and we create each submenu
      this.subClassesList = new ArrayList<Classe>();
      //... For each sub class we create the (sub)Classe and add it to the 
      //subclassList
      for(Element subclasse : tmpSubClasses)
        this.subClassesList.add(new Classe(subclasse));
    }
  }

  /**
   * Returns the property  key linked to this Classe in the multilanguage file.
   * @return String - The property key linked to this Classe.
   */
  public String getMultiLanguageName() {
    return multiLanguageName;
  }

  /**
   * Returns the name of the table where the Classe informations are store.
   * @return String - The name of the table.
   */
  public String getTableName() {
    return tableName;
  }

  /**
   * Returns the path of the picture of this classe.
   * @return String - The path of the picture of this picture.
   */
  public String getImagePath() {
    return imagePath;
  }

  /**
   * Returns the minimum level required to "be" this Classe.
   * @return int - The minimum level required for this classe.
   */
  public int getMinLvl() {
    return minLvl;
  }

  /**
   * Returns the max. level which can be reached with this Classe.
   * @return int - The maximum level.
   */
  public int getMaxLvl() {
    return maxLvl;
  }

  /**
   * Returns the subClasse list.
   * @return List<Classe> - The subclass list.
   */
  public List<Classe> getSubClassesList() {
    return subClassesList;
  }

  /**
   * Returns an array containing all the factionId (String) which can be this 
   * Classe.
   * @return The factionId array.
   */
  public String[] getFactionsId() {
    return factionsId;
  }

  /**
   * Returns the unique identifier linked to the Classe.
   * @return The id linked to the classe.
   */
  public String getId()
  {
    return this.id;
  }
  /**
   * Returns the Classe name according to the language currently used.
   * @return String - The Classe name according to the current language.
   */
  public String toString()
  {
    return MultiLanguages.getProperties(multiLanguageName);
  }

  /**
   * Returns true if the classe allows the faction given in parameter.
   * @param faction - The faction candidate.
   * @return True if this classe allows the Faction <i>faction</i>.
   */
  public boolean factionAuthorized(Faction faction)
  {
    for(String id : this.factionsId)
      if(id.equalsIgnoreCase(faction.getId()))
        return true;
    return false;
  }
}